package com.settlement.system.converter;

import com.alibaba.excel.converters.date.DateStringConverter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.CellData;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.DateUtils;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.util.Date;


/**
 * @author author
 */
public class DateFormatYearMonthPeriodConvert extends DateStringConverter {

    @Override
    public Date convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws ParseException {
        String value = cellData.getStringValue();
        if (!StringUtils.isEmpty(value)) {
            return DateUtils.parseDate(value, "yyyyMM月");
        }
        return null;
    }

    @Override
    public WriteCellData convertToExcelData(Date value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        if (value != null) {
            String date = DateUtils.format(value, "yyyyMM月");
            return new WriteCellData(date);
        }
        return super.convertToExcelData(value, contentProperty, globalConfiguration);
    }
}
